<?php

namespace Yolo\Utils;

use Psr\Log\LogLevel;
use Symfony\Component\Console\Output\ConsoleOutput;

class Console
{
    /**
     * Log a message to the console.
     * @param string $message The message to log.
     * @param string $level The log level.
     * @return void
     */
    public static function log(string $message, string $level = 'info'): void
    {
        $tag = match ($level) {
            LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL => 'error',
            LogLevel::ERROR => 'fg=red',
            LogLevel::WARNING, LogLevel::NOTICE => 'comment',
            default => 'info',
        };

        $template = sprintf('<%s>[%s]</>', $tag, strtoupper($level));

        $now = DateTime::getCurrentDateTimeWithMs();

        $message = sprintf($template . '[%s] %s', $now, $message);

        $log = new ConsoleOutput();
        $log->writeln($message);
    }
}
